package com.guli.mall.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.member.mapper.MemberReceiveAddressMapper;
import com.guli.mall.member.entity.MemberReceiveAddressEntity;
import com.guli.mall.member.service.MemberReceiveAddressService;
import org.springframework.util.ObjectUtils;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils<MemberReceiveAddressEntity> queryPage(Integer currentPage, Integer size, Map<String, Object> params) {

        Page<MemberReceiveAddressEntity> page = new Page<>(currentPage, size);

        Page<MemberReceiveAddressEntity> resultPage = this.page(page);


        return new PageUtils<>(resultPage);
    }

    @Override
    public List<MemberReceiveAddressEntity> getMemberReceiveAddressByMemberId(Long memberId) {

        QueryWrapper<MemberReceiveAddressEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("member_id", memberId);

        List<MemberReceiveAddressEntity> list = this.list(wrapper);

        for (int i = 0; i < list.size(); i++) {

            if (!ObjectUtils.isEmpty(list.get(i).getDefaultStatus()) &&list.get(i).getDefaultStatus() == 1) {
                MemberReceiveAddressEntity addressEntity = list.get(0);
                list.set(0, list.get(i));
                list.set(i, addressEntity);
            }
        }

        return list;
    }

    @Override
    public MemberReceiveAddressEntity calculateCarriage(Long memberReceiveAddressId) {

        //TODO 调用计算运费的接口
        int i = new Random().nextInt(20);

        MemberReceiveAddressEntity member = this.getById(memberReceiveAddressId);

        member.setCarriage(new BigDecimal(member.getPhone().substring(member.getPhone().length()-2)));

        return member;
    }


}